package com.bot.adms.dao.impl;

import com.bot.adms.dao.IApplyContentSpScDetailVideoDao;
import com.bot.adms.model.entity.ApplyContentSpScDetailVideo;
import com.bot.adms.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 投放申请视频信息表持久层接口实现
 *@author ：刘威林
 */
public class ApplyContentSpScDetailVideoDaoImpl implements IApplyContentSpScDetailVideoDao {
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;
    /**
     * 添加投放申请视频信息
     * @param applyContentSpScDetailVideo 投放申请视频信息：包含：投放申请视频信息id,视频文件路径，视频时长，视频分辨率
     * @return Integer 添加成功的数量
     */
    @Override
    public Integer insertApplyContentSpScDetailVideo(ApplyContentSpScDetailVideo applyContentSpScDetailVideo) {
        //获取数据库
        try {
            //1.获取Connection连接
            conn= JdbcUtils.getConnection();
            //2.通过连接获取statement对象
            stmt=conn.createStatement();
            //3.SQL语句
            String sql="insert into apply_content_sp_sc_detail_video(video_url,total_time,resolution,apply_content_sp_sc_detail_id) values('"
                    +applyContentSpScDetailVideo.getVideoUrl()+"',"
                    +applyContentSpScDetailVideo.getTotalTime()+","
                    +applyContentSpScDetailVideo.getResolution()+","
                    +applyContentSpScDetailVideo.getApplyContentSpScDetailId()+");";
            //4.执行SQL语句
            int count=stmt.executeUpdate(sql);
            //5.返回执行条数
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JdbcUtils.close(conn,stmt);
        }


    }

    /**
     * 修改投放申请视频信息
     * @param applyContentSpScDetailVideo 投放申请视频信息：包含：投放申请视频信息id,视频文件路径，视频时长，视频分辨率
     * @return Integer 添加成功的数量
     */
    @Override
    public Integer updateApplyContentSpScDetailVideo(ApplyContentSpScDetailVideo applyContentSpScDetailVideo) {
        try {
            //1.获取Connection连接
            conn = JdbcUtils.getConnection();
            //2.通过连接获取statement对象
            stmt = conn.createStatement();
            //3.SQL语句
            String sql="update apply_content_sp_sc_detail_video set video_url='" +applyContentSpScDetailVideo.getVideoUrl()+"'," +
                    "total_time=" +applyContentSpScDetailVideo.getTotalTime()+"," +
                    "resolution=" +applyContentSpScDetailVideo.getResolution()+"." +
                    "where id=" +applyContentSpScDetailVideo.getId()+";";

            //4.执行SQL语句
            int count=stmt.executeUpdate(sql);
            //5.返回执行条数
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JdbcUtils.close(conn, stmt);
        }
    }

    /**
     * 获取投放申请视频信息
     * @param id 投放申请视频信息id
     * @return  ApplyContentSpScDetailVideo 投放申请视频信息：包含：投放申请视频信息id,视频文件路径，视频时长，视频分辨率
     */
    @Override
    public Integer deleteApplyContentSpScDetailVideo(Long id) {
        try {
            //1.获取Connection连接
            conn = JdbcUtils.getConnection();
            //2.通过连接获取statement对象
            stmt = conn.createStatement();
            //3.SQL语句
            String sql="delete from apply_content_sp_sc_detail_video where id="+id+";";
            //4.执行SQL语句
            int count=stmt.executeUpdate(sql);
            //5.返回执行条数
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn,stmt);
        }
    }

    /**
     * 获取投放申请视频信息
     * @param id 投放申请视频信息id
     * @return  ApplyContentSpScDetailVideo 投放申请视频信息：包含：投放申请视频信息id,视频文件路径，视频时长，视频分辨率
     */
    @Override
    public ApplyContentSpScDetailVideo getApplyContentSpScDetailVideoById(Long id) {
        ApplyContentSpScDetailVideo applyContentSpScDetailVideo = null;
        try {
            //1.获取Connection连接
            conn = JdbcUtils.getConnection();
            //2.通过连接获取statement对象
            stmt = conn.createStatement();
            //3.SQL语句
            String sql="select * from apply_content_sp_sc_detail_video where id="+id+";";
            //4.遍历结果
            rs = stmt.executeQuery(sql);
            while (rs.next()){
                applyContentSpScDetailVideo = new ApplyContentSpScDetailVideo();
                applyContentSpScDetailVideo.setId(rs.getLong("id"));
                applyContentSpScDetailVideo.setVideoUrl(rs.getString("video_url"));
                applyContentSpScDetailVideo.setTotalTime(rs.getLong("total_time"));
                applyContentSpScDetailVideo.setResolution(rs.getLong("resolution"));
                applyContentSpScDetailVideo.setApplyContentSpScDetailId(rs.getLong("apply_content_sp_sc_detail_id"));

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn,stmt,rs);
        }
        return applyContentSpScDetailVideo;
    }

    /**
     * 查询投放申请视频信息
     * @param applyContentSpScDetailVideo 投放申请视频信息：包含：投放申请视频信息id,视频文件路径，视频时长，视频分辨率
     * @return List<ApplyContentSpScDetailVideo> 投放申请视频信息集合：包含：投放申请视频信息id,视频文件路径，视频时长，视频分辨率
     */
    @Override
    public List<ApplyContentSpScDetailVideo> searchApplyContentSpScDetailVideo(ApplyContentSpScDetailVideo applyContentSpScDetailVideo) {
        List<ApplyContentSpScDetailVideo> applyContentSpScDetailVideoList = new ArrayList<ApplyContentSpScDetailVideo>();
        try {
            //1.获取Connection连接
            conn = JdbcUtils.getConnection();
            //2.通过连接获取statement对象
            stmt = conn.createStatement();
            //3.SQL语句
            String sql="select * from apply_content_sp_sc_detail_video where 1=1";
            if(applyContentSpScDetailVideo.getId()!=null){
                sql+=" AND `id` = " +applyContentSpScDetailVideo.getId();
            }
            if(applyContentSpScDetailVideo.getVideoUrl()!=null && !applyContentSpScDetailVideo.getVideoUrl().isEmpty()){
                sql+=" AND `video_url` = '" +applyContentSpScDetailVideo.getVideoUrl()+"'";
            }
            if(applyContentSpScDetailVideo.getTotalTime()!=null){
                sql+=" AND `total_time` = " +applyContentSpScDetailVideo.getTotalTime();
            }
            if(applyContentSpScDetailVideo.getResolution()!=null){
                sql+=" AND `resolution` = " +applyContentSpScDetailVideo.getResolution();
            }
            if(applyContentSpScDetailVideo.getApplyContentSpScDetailId()!=null){
                sql+=" AND `apply_content_sp_sc_detail_id` = " +applyContentSpScDetailVideo.getApplyContentSpScDetailId();
            }
            //4.遍历结果
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                ApplyContentSpScDetailVideo applyContentSpScDetailVideo1 = new ApplyContentSpScDetailVideo();
                applyContentSpScDetailVideo1.setId(rs.getLong("id"));
                applyContentSpScDetailVideo1.setVideoUrl(rs.getString("video_url"));
                applyContentSpScDetailVideo1.setTotalTime(rs.getLong("total_time"));
                applyContentSpScDetailVideo1.setResolution(rs.getLong("resolution"));
                applyContentSpScDetailVideo1.setApplyContentSpScDetailId(rs.getLong("apply_content_sp_sc_detail_id"));
                applyContentSpScDetailVideoList.add(applyContentSpScDetailVideo1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JdbcUtils.close(conn,stmt,rs);
        }
        return applyContentSpScDetailVideoList;
    }
}
